package org.budo.weixin.wxsuite;

import java.util.Map;

import org.budo.support.http.client.Request;
import org.budo.support.http.client.Response;
import org.budo.support.lang.util.MapUtil;
import org.budo.support.lang.util.StringUtil;
import org.budo.support.slf4j.Slf4j;
import org.slf4j.Logger;

import com.alibaba.fastjson.JSON;

/**
 * @author lmw
 */
public class WxSuiteApi {
    private static final Logger log = Slf4j.getLogger();

    // 获取应用套件令牌
    public static String getSuiteToken(String suite_id, String suite_secret, String suite_ticket) {
        Request request = new Request();
        request.setUrl("https://qyapi.weixin.qq.com/cgi-bin/service/get_suite_token");
        request.setMethod(Request.POST);
        Map data = MapUtil.newMap("suite_id", suite_id, "suite_secret", suite_secret, "suite_ticket", suite_ticket);
        request.setRequestBody(JSON.toJSONBytes(data));
        Response response = request.execute();
        String result = response.getBody();

        log.info("getSuiteToken result={}", result);
        Map<String, Object> map = JSON.parseObject(result);

        String suite_access_token = StringUtil.toString(map.get("suite_access_token"));
        // String expires_in = StringUtil.toString(map.get("expires_in"));
        if (StringUtil.isEmpty(suite_access_token)) {
            log.error("获取微信suite_access_token失败,response={}", response);
        }
        return suite_access_token;
    }

    //获取企业号的永久授权码
    public static Map<String, Object> getPermanentCode(String suite_access_token, String suite_id, String auth_code) {
        Request request = new Request();
        request.setUrl("https://qyapi.weixin.qq.com/cgi-bin/service/get_permanent_code?suite_access_token=" + suite_access_token);
        request.setMethod(Request.POST);
        Map data = MapUtil.newMap("suite_id", suite_id, "auth_code", auth_code);
        request.setRequestBody(JSON.toJSONBytes(data));
        Response response = request.execute();
        String result = response.getBody();
        log.info("get_permanent_code result={}", result);
        Map<String, Object> map = JSON.parseObject(result);
        return map;
    }

    //获取企业号的预授权码
    public static String getPreAuthCode(String suite_access_token, String suite_id) {
        Request request = new Request();
        request.setUrl("https://qyapi.weixin.qq.com/cgi-bin/service/get_pre_auth_code?suite_access_token=" + suite_access_token);
        request.setMethod(Request.POST);
        Map data = MapUtil.newMap("suite_id", suite_id);
        request.setRequestBody(JSON.toJSONBytes(data));
        Response response = request.execute();
        String result = response.getBody();
        log.info("getPreAuthCode result={}", result);
        Map<String, Object> map = JSON.parseObject(result);
        String pre_auth_code = StringUtil.toString(map.get("pre_auth_code"));
        return pre_auth_code;
    }

    // 获取企业号的access_token
    public static String getCorpToken(String suite_access_token, String suite_id, String auth_corpid, String permanent_code) {
        Request request = new Request();
        request.setUrl("https://qyapi.weixin.qq.com/cgi-bin/service/get_corp_token?suite_access_token=" + suite_access_token);
        request.setMethod(Request.POST);
        Map data = MapUtil.newMap("suite_id", suite_id, "auth_corpid", auth_corpid, "permanent_code", permanent_code);
        request.setRequestBody(JSON.toJSONBytes(data));
        Response response = request.execute();
        String result = response.getBody();
        log.info("getSuiteToken result={}", result);
        Map<String, Object> map = JSON.parseObject(result);
        String access_token = StringUtil.toString(map.get("access_token"));
        if (StringUtil.isEmpty(access_token)) {
            log.error("获取getCorpToken失败,response={}", response);
        }
        return access_token;
    }

    //获取企业号登录用户信息 (业务设置URL使用)
    public static Map<String, Object> getLoginInfo(String suite_access_token, String code) {
        Request request = new Request();
        request.setUrl("https://qyapi.weixin.qq.com/cgi-bin/service/get_login_info?access_token=" + suite_access_token);
        request.setMethod(Request.POST);
        Map data = MapUtil.newMap("auth_code", code);
        request.setRequestBody(JSON.toJSONBytes(data));
        Response response = request.execute();
        String result = response.getBody();
        log.info("get_login_info result={}", result);
        Map<String, Object> map = JSON.parseObject(result);
        return map;
    }

    //根据code获取成员信息
    public static Map<String, Object> getUserInfo(String access_token, String code) {
        Request request = new Request();
        request.setUrl("https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=" + access_token + "&code=" + code);
        request.setMethod(Request.GET);
        Response response = request.execute();
        String result = response.getBody();
        log.info("getUserInfo result={}", result);
        Map<String, Object> map = JSON.parseObject(result);
        return map;
    }

}
